package com.s3.s3demo.aws;

import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.ListObjectsRequest;
import software.amazon.awssdk.services.s3.model.ListObjectsResponse;

import java.nio.file.Path;

/**
 * 获取存储桶下的对象列表
 * 下载对象文件
 */
public class GetObjects extends AwsAuth {

    public static void main(String[] args) {
        try (S3Client s3Client = generateS3Client()) {
            listObjects(s3Client);
            getObject(s3Client);
        }
    }

    private static void listObjects(S3Client s3Client) {
        ListObjectsResponse response = s3Client.listObjects(ListObjectsRequest.builder().build());
        response.contents().forEach(o -> System.out.println(o.toString()));
    }

    private static void getObject(S3Client s3Client) {
        String bucketName = "kestra";
        String key = "kestra.yaml";
        String localFile = "F:\\s3test\\test.yaml";
        s3Client.getObject(GetObjectRequest.builder().bucket(bucketName).key(key).build(),
                Path.of(localFile));
    }

}
